/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.loja.view.component.composition;

import br.com.loja.view.component.factory.ViewHadler;
import br.com.loja.view.component.factory.EditorToMany;
import br.com.loja.view.swing.model.EntityDefaultTableModel;
import java.awt.Component;
import java.util.Collections;
import java.util.List;
import javax.swing.JTable;
import javax.swing.SwingUtilities;

/**
 *
 * @author DavidLuiz
 */
public class EntityToMany<T> extends JTable implements ViewHadler<List<T>>, EditorToMany {

    private final EntityDefaultTableModel<T> tableModel;

    public EntityToMany(Class<T> clazz) {
        this.tableModel = new EntityDefaultTableModel<>(clazz);
        this.tableModel.setEntities(Collections.EMPTY_LIST);
    }

    @Override
    public void setValueModel(List<T> value) {
        tableModel.getEntities().clear();
        tableModel.setEntities(value);
        setModel(tableModel);
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                updateUI();
            }
        });
    }

    @Override
    public List<T> getValueModel() {
        return tableModel.getEntities();
    }

    @Override
    public Component getEditorToMany() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}
